﻿using PortfolioApp.DataModels;
using PortfolioApp.Web.DbAdapters;
using PortfolioApp.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace PortfolioApp.Web.Controllers
{
    public class PostController : ApiController
    {
        private IPortfolioAppAdapter _adapter = new PortfolioAppAdapter();

        // GET api/post/0
        public IHttpActionResult Get(int id)
        {
            PostDetailVM vm = new PostDetailVM();
            vm.Post = _adapter.GetPost(id);
            vm.UserName = _adapter.GetUserName(vm.Post.ApplicationUserId);
            return Ok(vm);
        }

        public IHttpActionResult Post([FromBody]Post post)
        {
            // For now, manually setting user until Auth is working
            post.ApplicationUserId = "40ba2bd1-c737-4d60-ae63-dc283a7d640b";
            post = _adapter.CreatePost(post);
            return Ok(post);

        }

        // PUT api/post
        public IHttpActionResult Put([FromBody]Post post)
        {
            post = _adapter.UpdatePost(post);
            return Ok(post);
        }

        // DELETE api/delete
        public IHttpActionResult Delete(int id)
        {
            Post post = _adapter.GetPost(id);
            post = _adapter.DeletePost(post);
            return Ok();
        }
    }
}
